package com.woniuxy.dr_mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.dr_mall.entity.DrAddress;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
public interface DrAddressMapper extends BaseMapper<DrAddress> {
    /**
     * 新增地址
     * @param drAddress
     */
    @Insert("insert into dr_address(userId,receiver,telephone,province,city,district,address,status) values(#{userId},#{receiver},#{telephone},#{province},#{city},#{district},#{address},#{status})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void addAddress(DrAddress drAddress);

    /**
     * 根据id删除地址
     * @param id
     */
    @Delete("delete from dr_address where id = #{id}")
    void delById(Integer id);

    /**
     * 设置该用户的所有地址的状态为'n',和设置status为'y'搭配使用
     * @param userId
     */
    @Update("update dr_address set status='n' where userId=#{userId}")
    void editStatusIsNByUserId(@Param("userId")Integer userId);

    /**
     * 根据地址id设置当前id对应的address为默认地址
     * @param id
     */
    @Update("update dr_address set status='y' where id = #{id}")
    void editStatusIsYById(@Param("id")Integer id);

    /**
     * 获取当前用户的所有地址
     * @param userId
     * @return
     */
    @Select("select * from dr_address where userId = #{userId}")
    List<DrAddress> getAllByUserId(@Param("userId")Integer userId);

    @Update("update dr_address set receiver=#{receiver},telephone=#{telephone},province=#{province},city=#{city},district=#{district},address=#{address},status=#{status}" +
            " where id = #{id}")
    void editAddress(DrAddress address);


}
